package com.lupan.tomcatStudy.mongodb.districtToBounds;

import com.alibaba.fastjson.JSON;
import com.lupan.tomcatStudy.mongodb.districtToBounds.entity.City;
import com.lupan.tomcatStudy.mongodb.districtToBounds.entity.Province;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ContextConfiguration(locations = {"/foundation-my-lrs.xml"})
public class ConvertServiceTestMyMongodbSingleCity extends AbstractTestNGSpringContextTests {

    @Autowired
    ConvertService convertService;

    private static Logger logger = Logger.getLogger(ConvertServiceTestMyMongodbSingleCity.class);

    @Test
    public void convertAllProvince() {

//        Scanner scanner = new Scanner(System.in);
//        while (true) {

            //统计所有未成功转换的城市、省份名称
            List<Object> errorList = new ArrayList<>();
            int count = 0;
            int successCount = 0;

            //获取要转换的city对象
            City city = JSON.parseObject("\n" +
                    "{\"cityName\":\"海东市\",\"code\":\"6321\",\"province\":{\"code\":\"630000\",\"provinceName\":\"青海省\"}}", City.class);
            assert city != null;
            Province province = city.getProvince();
            logger.info("\n" + province.getProvinceName() + " ******开始处理！*******");


//            //获取每个省份转换后的范围信息
//            JSONObject convertedProvinceMap = ConvertUtil.convert(province.getProvinceName(), Constant.LEAVE_PROVINCE);
//            count++;
//            //判断是否转换成功
//            if(Integer.parseInt((String)convertedProvinceMap.get("count"))>0){
//
//                //存入数据库
//                convertService.saveBounds(JSON.toJSONString(convertedProvinceMap.getJSONArray("districts")));
//                successCount++;
//            }else{
//                errorList.add(province);
//            }
            //获取每个省份的城市列表
//        List<City> cityList = convertService.getCitisByPrinviceName(province.getProvinceName());
            //遍历每个城市
//        for (City city : cityList) {
            city.setProvince(province);
            //获取每个城市转换后的范围信息
            Map convertedCityMap = ConvertUtil.convert(city.getCityName(), Constant.LEAVE_CITY);
            count++;
            if (Integer.parseInt((String) convertedCityMap.get("count")) > 0) {
                //存入数据库
                convertService.saveBounds(JSON.toJSONString(convertedCityMap.get("districts")), city);
                successCount++;
            } else {
                logger.error("未找到边界数据！");
//                    System.out.println("未找到边界数据！");
                errorList.add(city);
            }

//                System.out.println(city.getCityName()+"  ------处理完毕！------");
            logger.info(city.getCityName() + "  ------处理完毕！------");
//        }
//        }

            //打印统计结果
            System.out.println("转换结果：");
            System.out.println(String.format("共转换了%s个省、市数据，成功%s个，错误%s个。", count, successCount, errorList.size()));
            if (errorList.size() > 0) {
//            System.out.println("错误信息如下：");
                logger.info("错误信息如下：");
                int counter = 0;
                for (Object o : errorList) {
                    counter++;
                    if (o instanceof City) {
                        City errorCity = (City) o;
                        System.out.println(String.format("%s   %s.%s", counter, errorCity.getProvince().getProvinceName(), city.getCityName()));
                    } else if (o instanceof Province) {
                        Province errorProvince = (Province) o;
                        System.out.println(String.format("%s   %s", counter, errorProvince.getProvinceName()));
                    } else {
                        assert false : "未知类型错误！";
                    }
                }
            }
//        }
    }
}